<!-- 自动补全输入框 -->
<template>
  <div class="flex gap-4">
    <div>
      <!-- 字体紧贴框 
       <div class="sub-title my-2 text-sm text-gray-600"> 
         {{ title }} 
       </div>  -->
       
       <!-- 字体居中 -->
      <p>{{ title }}</p>
      <el-autocomplete
        v-model="state1"
        :fetch-suggestions="querySearch"
        clearable
        class="inline-input w-50"
        placeholder="请输入关键字"
        @select="handleSelect"
      />
    </div>
  </div>
</template>

<script lang="js" setup>
import { onMounted, ref } from 'vue'

// 属性
const props = defineProps({
  title: {
    type: String,
    default: '搜索框标题',
  },
})


const state1 = ref('')
const state2 = ref('')

const restaurants = ref([])
const querySearch = (queryString, cb) => {
  const results = queryString
    ? restaurants.value.filter(createFilter(queryString))
    : restaurants.value
  // call callback function to return suggestions
  cb(results)
}
const createFilter = (queryString) => {
  return (restaurant) => {
    return (
      restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
    )
  }
}
const loadAll = () => {
  return [
    { value: 'vue', link: 'https://github.com/vuejs/vue' },
    { value: 'element', link: 'https://github.com/ElemeFE/element' },
    { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
    { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
    { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
    { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
    { value: 'babel', link: 'https://github.com/babel/babel' },
  ]
}

const handleSelect = (item) => {
  console.log(item)
}

onMounted(() => {
  restaurants.value = loadAll()
})
</script>


<style lang="scss" scoped>
// 自动输入框的父容器
.gap-4{
  display: flex;
  // 元素居中
  justify-content: center;
}
</style>